package cn.edu.zust.se.mvcdb.filter;

import cn.edu.zust.se.mvcdb.vo.User;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Date;
@WebFilter(filterName = "logFilter",urlPatterns = "/*")
public class LogFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest req=(HttpServletRequest)servletRequest;
        User u=(User)req.getSession().getAttribute("user");
        String name=u==null?"anno":u.getName();
        String uri=((HttpServletRequest) servletRequest).getRequestURI();
        System.out.println((new Date()).toGMTString()+"  "+name+"访问了"+uri);
        filterChain.doFilter(servletRequest,servletResponse);
    }
}
